﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Tunnel_Cross_Section.menue_frm;

namespace Tunnel_Cross_Section.option_frm
{
    public partial class select_period_frm : Office2007Form
    {
        Mfrm mfrm;
        do_section_frm section_frm;
        DataBase m_db;
        string all_ids;
        int id;
        public select_period_frm(Mfrm m,do_section_frm frm,DataBase db)
        {
            InitializeComponent();
            section_frm = frm;
            m_db = db;
            mfrm = m;
            Load_ComboBox();
            radioButton1.Checked = true;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                comboBox.Enabled = true;
                Input.Enabled = false;

            }
            else
            {
                comboBox.Enabled = false;
                Input.Enabled = true;
            }
        }


        void Load_ComboBox()
        {
            string sql = "Select [期数],[id] from [projects] where [区间]='" + mfrm.cur_prj.section_name + "'";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];

            all_ids = dt.Rows[0][0].ToString();
            id = Convert.ToInt32(dt.Rows[0][1]);
            if (all_ids.Length == 0)
            {
                radioButton1.Enabled = false;
                radioButton2.Checked = true;
                labelX1.Visible = true;
                return;
            }
            string[] ids = dt.Rows[0][0].ToString().Split(',');
            comboBox.DataSource = ids;
            string nn = ids[ids.Length - 1];
            if (nn.Length > 0)
            {
                int aa = Convert.ToInt32(nn);
                Input.MinValue = aa + 1;
            }

        }


        //点击了确定
        private void btn_ok_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                int i = comboBox.SelectedIndex;
                if(i<0)
                {
                    message_frm message = new message_frm("请选择期数!", "错误", 3);
                    message.ShowDialog();
                    message.Close();
                    return;
                }
                section_frm.no = Convert.ToInt32(comboBox.Items[i]);
                mfrm.cur_prj.id = id;
            }
            else
            {
                mfrm.cur_prj.no = Input.Value;
                section_frm.no = Input.Value;
                mfrm.cur_prj.id = id;
                all_ids = all_ids + "," + Input.Value.ToString();
                string sql = "update [projects] set [期数]='" + all_ids + "' where [id]=" + id;
                m_db.ExecuteSQL(sql);
                mfrm.Label.Text = "新建期数成功";
            }
            this.Close();

        }

        private void btn_no_Click(object sender, EventArgs e)
        {
            section_frm.no = 0;
            this.Close();
        }
    }
}
